package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "品牌")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {
    @Autowired
    IBrandService brandService;

    /**
     * 品牌查询
     * @param brand
     * @return
     */
    @Operation(summary = "分页")
    @GetMapping("/list")
    public TableDataInfo list(Brand brand){
        //开启分页
        startPage();
        List<Brand> brandList = brandService.selectBrandList(brand);
        return getDataTable(brandList);
    }

    /**
     * 通过id查询
     * @param id
     * @return 一个对象
     */
    @Operation(summary = "ID查询")
    @GetMapping("/{id}")
    public AjaxResult getBrandById(@PathVariable("id") Long id){
        Brand brand = brandService.selectBrandById(id);
        return success(brand);
    }

    /**
     * 增加品牌
     * @param brand
     * @return  影响的行数：＞0表示成功，＜0表示失败
     */
    @Operation(summary = "增加品牌")
    @PostMapping
    public AjaxResult addBrand(@RequestBody @Validated Brand brand){
        brand.setCreateBy(SecurityUtils.getUsername());
        int result = brandService.insertBrand(brand);
        return toAjax(result);
    }

    /**
     * 删除品牌
     * @param ids
     * @return  影响的行数：＞0表示成功，＜0表示失败
     */
    @Operation(summary = "删除品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") List<Long> ids){
        int result = brandService.deleteByIds(ids);
        return toAjax(result);
    }

    /**
     * 通过ID修改品牌信息
     * @param brand
     * @return 影响的行数：＞0表示成功，＜0表示失败
     */
    @Operation(summary = "修改品牌")
    @PutMapping
    public AjaxResult updateBrandById(@RequestBody @Validated Brand brand){
        brand.setUpdateBy(SecurityUtils.getUsername());
        int result = brandService.updateBrandById(brand);
        return toAjax(result);
    }

    @Operation(summary = "查询全部")
    @GetMapping
    public TableDataInfo selectListAll(){
        List<Brand> list = brandService.selectListAll();
        return getDataTable(list);
    }


    //===================给前端使用============================
    @InnerAuth //给内部调用使用
    @Operation(summary = "获取全部品牌")
    @GetMapping("/getBrandList")
    public R<List<Brand>> getBrandAllList(){
        return R.ok(brandService.selectListAll());
    }
}
